import { defineConfig, loadEnv } from 'vite';
import { resolve } from 'path';
import createVitePlugins from './vite/plugins';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path from 'path';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV, VITE_APP_BASE } = env;
  // 判断是打生产环境包
  const isProd = VITE_APP_ENV === 'production';

  // 根据是否生产环境，动态设置压缩配置
  const buildConfig = {
    target: 'esnext',
    minify: isProd ? 'terser' : 'esbuild', // 根据环境选择压缩工具
  };

  // 如果是生产环境，添加Terser的配置
  if (isProd) {
    buildConfig.terserOptions = {
      compress: {
        drop_console: true, // 删除 console
        drop_debugger: true, // 删除 debugger
      },
      format: {
        comments: false, // 删除所有注释
      },
    };
    buildConfig.rollupOptions = {
      output: {
        manualChunks: {
          'element-plus': ['element-plus'],
          '@smallwei/avue': ['@smallwei/avue'],
        },
      },
    };
  }
  return defineConfig({
    base: VITE_APP_BASE,
    define: {
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: true,
      __INTLIFY_PROD_DEVTOOLS__: false,
    },
    plugins: [
      // vue(),

      //添加jsx/tsx支持
      vueJsx({}),
      createVitePlugins(env, command === 'build'),

      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    server: {
      port: 2888,
      proxy: {
        '/api': {
        //   target: 'http://127.0.0.1:8742',
          target: 'http://192.168.1.211:8742',
          // target: 'https://saber3.bladex.cn/api',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },
    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        '@': resolve(__dirname, './src'),
        components: resolve(__dirname, './src/components'),
        styles: resolve(__dirname, './src/styles'),
        utils: resolve(__dirname, './src/utils'),
      },
      extensions: ['.js', '.vue', '.json', '.ts'], // 使用路径别名时想要省略的后缀名，可以自己 增减
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          additionalData: `@use "@/styles/variables.scss" as *;`,
          //   additionalData: `@use "@/styles/global.scss" as *;`,
        },
      },
    },

    build: buildConfig,
    optimizeDeps: {
      esbuildOptions: {
        target: 'esnext',
      },
      include: ['./vuedraggable/dist/vuedraggable.umd.js', 'quill'],
    },
  });
};
